class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, int> mm;
        for (int i = 0; i < nums.size(); i++) {
            int x = nums[i];
            if (mm.contains(x) && i - mm[x] <= k)
                return true;
            mm[x] = i;
        }
        return false;
    }
};